package com.peoit.honeycomb.application.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.peoit.honeycomb.application.ProjectNoticeApplication;
import com.peoit.honeycomb.domain.HoneycombProject;
import com.peoit.honeycomb.domain.ProjectNotice;
import com.peoit.honeycomb.domain.User;
import com.peoit.honeycomb.domain.exception.ProjectNotExistException;
import com.peoit.honeycomb.domain.exception.UserNotExistException;
import com.peoit.honeycomb.domain.repository.HibernateEntityRepository;
import com.peoit.honeycomb.dto.ProjectNoticeDto;

/**
 * 项目公告信息IMPL
 * 
 * @author zhenshis
 *
 */
@Service
public class ProjectNoticeApplicationImpl extends HibernateEntityRepository implements ProjectNoticeApplication {

	@Override
	@Transactional
	public void publishProjectNotice(ProjectNoticeDto dto) {
		HoneycombProject project = find(HoneycombProject.class, dto.getProjectId());
		if (project == null) {
			throw new ProjectNotExistException();
		}
		User user = find(User.class, dto.getUserId());
		if (user == null) {
			throw new UserNotExistException();
		}
		ProjectNotice notice = ProjectNoticeDto.toEntity(dto);
		notice.setProject(project);
		notice.setUser(user);
		create(notice);
	}

	@Override
	public List<ProjectNoticeDto> findProjectNotice(ProjectNoticeDto dto) {
		String whereSql = " project.id = ? order by createTime";
		Object[] queryParams = { dto.getProjectId() };
		List<ProjectNotice> notices = queryByWhereList(ProjectNotice.class, whereSql, queryParams);
		List<ProjectNoticeDto> dtos = new ArrayList<ProjectNoticeDto>();
		for (ProjectNotice notice : notices) {
			dtos.add(ProjectNoticeDto.toDto(notice));
		}
		return dtos;
	}

}
